//
// FileChannel_Linux.cpp
// Library: Foundation
// Package: Logging
// Module:  FileChannel_Linux
//
// Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// SPDX-License-Identifier:	BSL-1.0
//


#include "Poco/FileChannel.h"
#include "Poco/ArchiveStrategy.h"
#include "Poco/RotateStrategy.h"
#include "Poco/PurgeStrategy.h"
#include "Poco/DateTime.h"
#include "Poco/Exception.h"
#include "Poco/Environment.h"
#include "Poco/Format.h"

#include <sys/types.h>
#include <unistd.h>
#include <sstream>
#include <ctime>
#include <string>
#include <pthread.h>

using std::string;

namespace Poco {


string  FileChannel::getPrefix(LogPriority prio)
{
    string ret = m_prefixOption;
    Timestamp   ts;
    DateTime    dt = ts;
    // 需要倒序遍历
    auto itOpt = m_options.rbegin();
    for(;itOpt != m_options.rend();itOpt++)
    {
        switch(itOpt->m_option[1])
        {
            case 'p':(prio >= LOG_FATAL && prio <= LOG_TRACE)?ret.replace(itOpt->m_pos,itOpt->m_option.length(),PriorityString[prio]):0;break;
            case 'P':ret.replace(itOpt->m_pos,itOpt->m_option.length(),Poco::format("%d",getpid()));break;
            case 'I':ret.replace(itOpt->m_pos,itOpt->m_option.length(),Poco::format("%d",pthread_self()));break;
            case 'N':ret.replace(itOpt->m_pos,itOpt->m_option.length(),Environment::get("HOSTNAME"));break;
            case 'Y':ret.replace(itOpt->m_pos,itOpt->m_option.length(),Poco::format("%d",dt.year()));break;
            case 'm':ret.replace(itOpt->m_pos,itOpt->m_option.length(),Poco::format("%02d",dt.month()));break;
            case 'n':ret.replace(itOpt->m_pos,itOpt->m_option.length(),Poco::format("%d",dt.month()));break;
            case 'o':ret.replace(itOpt->m_pos,itOpt->m_option.length(),Poco::format("%2d",dt.month()));break;
            case 'd':ret.replace(itOpt->m_pos,itOpt->m_option.length(),Poco::format("%02d",dt.day()));break;
            case 'e':ret.replace(itOpt->m_pos,itOpt->m_option.length(),Poco::format("%2d",dt.day()));break;
            case 'f':ret.replace(itOpt->m_pos,itOpt->m_option.length(),Poco::format("%2d",dt.day()));break;
            case 'H':ret.replace(itOpt->m_pos,itOpt->m_option.length(),Poco::format("%02d",dt.hour()));break;
            case 'M':ret.replace(itOpt->m_pos,itOpt->m_option.length(),Poco::format("%02d",dt.minute()));break;
            case 'S':ret.replace(itOpt->m_pos,itOpt->m_option.length(),Poco::format("%02d",dt.second()));break;
            case 'i':ret.replace(itOpt->m_pos,itOpt->m_option.length(),Poco::format("%02d",dt.millisecond()));break;
            default:
            break;
        }
    }
    // now itOpt = m_ooptions.begin()
    return ret;
}

} // namespace Poco
